//
//  UserLoginView.swift
//  ZTWeiboo
//
//  Created by mac on 15/8/7.
//  Copyright © 2015年 YYL. All rights reserved.
//

import UIKit

protocol UserLoginViewDelegate : NSObjectProtocol {
    
    func userLoginViewWillRegister();
    func userLoginViewWillLogin();

}

class UserLoginView: UIView {

    weak var delegate : UserLoginViewDelegate?

    func clickRegisterBtn(){
        delegate?.userLoginViewWillRegister()
    }
    
    func clickLoginBtn(){
        delegate?.userLoginViewWillLogin()
    }
    
    override init(frame: CGRect) {
        super.init(frame : frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    func setupInfo(isHome : Bool , imageName : String , message : String){
        messageLable.text = message
        iconView.image = UIImage(named: imageName)
        houseIconView.hidden = !isHome
        
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
    }
    
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI_2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    private func setupUI(){
        addSubview(iconView)
        addSubview(houseIconView)
        addSubview(maskIconView)
        addSubview(messageLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        houseIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 224))
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant:100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 35))
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant:100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 35))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-40)-[subView]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views : ["maskView":maskIconView,"subView" : registerBtn]))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
    }
    
    
    private lazy var iconView : UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imgView
        }()
    
    private lazy var houseIconView : UIImageView = {
        let imgView = UIImageView(image : UIImage(named: "visitordiscover_feed_image_house"))
        
        return imgView
        }()
    
    
    private lazy var maskIconView : UIImageView = {
        let  imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imgView
    }()
    
    private lazy var messageLable : UILabel = {
        let lable = UILabel()
        
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.tintColor = UIColor.orangeColor()
        lable.font = UIFont.systemFontOfSize(17)
        lable.numberOfLines = 0
        lable.sizeToFit()
        
        return lable
    }()
    
    private lazy var registerBtn : UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        return btn
    }()
    
    private lazy var loginBtn :UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        return btn
    }()
    
    
}
